<template>
  <div class="login">
 
   <van-form @submit="onSubmit">
  <van-cell-group inset>
    <van-field
      v-model="username"
      name="userName"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="password"
      type="password"
      name="password"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
  </van-cell-group>
  <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">
      提交
    </van-button>
  </div>
</van-form>

  </div>
</van-form>

  </div>
</template>

<script>
import {login} from '@/api/user';
import {setToken} from '@/utils/auth.js';
import { Toast } from 'vant';
export default {
  data() {
    return {
      username:"zqq",
      password:"123",
    };
  },
  computed: {},
  watch: {},

  methods: {
 async onSubmit(val){
      console.log({...val});
      const res = await login({
        userName:this.username,
        password:this.password,
      });
      console.log(res);
      
      if(res.code =="success"){

       //  保存账号  昵称 头像
        setToken(res.token);
        localStorage.setItem('token',res.token);
        //  跳转登录
        Toast('登录成功');

        this.$router.push("/home");
      }else{
        alert(res.message);
      }
   },
  },
  created() {},
  mounted() {},
  components: {},
};
</script>
<style scoped></style>
